//
//  LXDTypeViewController.m
//  BLDMusics
//
//  Created by lxd on 16/3/28.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "LXDTypeViewController.h"

@interface LXDTypeViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UIImageView *imagView;

@property (nonatomic, retain) NSMutableArray *modelArr;

@property (nonatomic, retain) NSString *str;
@end

@implementation LXDTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = YES;
    [self createTableView];
    [self dataHandle];
//    self.automaticallyAdjustsScrollViewInsets = NO;
}


- (void)createTableView {
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, SCREEN_WIDTH, [UIScreen mainScreen].bounds.size.height + 5) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.contentOffset = CGPointMake(0, -10);
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.contentInset = UIEdgeInsetsMake(250, 0, 0, 0);
    
    // 头视图
    self.imagView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -250, SCREEN_WIDTH, 250)];
    [self.tableView addSubview:_imagView];
    [_imagView sd_setImageWithURL:[NSURL URLWithString:self.pic]];
    
    // 取消navigationbar下面的一条素的黑线
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        NSArray *list = self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView = (UIImageView *)obj;
                NSArray *list2 = imageView.subviews;
                for (id obj2 in list2) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView2 = (UIImageView *)obj2;
                        imageView2.hidden = YES;
                    }
                }
            }
        }
    }

    [_tableView registerClass:[BYJCarouselTableViewCell class] forCellReuseIdentifier:@"carouse"];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}




#pragma mark - 实现导航栏的隐藏
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // tableView滑动时，图片的放大缩小
    CGFloat yOffset = scrollView.contentOffset.y;

    CGFloat alpha = (yOffset + 250) / 250;

    // 实现navigationBar的滑动隐藏
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor colorWithRed:95 / 255.0 green:217 / 255.0 blue:238 / 255.0 alpha:1] colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
}
- (UIImage *)imageWithColor:(UIColor *)color {
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    // 开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文获取图片
    UIImage *theImag = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext();
    return theImag;
}

#pragma mark - 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_modelArr.count) {
        return _modelArr.count + 1;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
        
        UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        playButton.frame = CGRectMake(10, 20, 30, 30);
        [playButton setImage:[UIImage imageNamed:@"PlayPressed"] forState:UIControlStateNormal];
        [playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:playButton];
        
        UILabel *playLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, 100, 30)];
        playLabel.text= @"播放全部";
        [cell addSubview:playLabel];
        
        UIButton *manageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        manageButton.frame = CGRectMake(SCREEN_WIDTH - 50, 20, 50, 50);
        [manageButton setImage:[UIImage imageNamed:@"audioeffect_equalizer_checked"] forState:UIControlStateNormal];
        [manageButton addTarget:self action:@selector(manageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:manageButton];
        
        return cell;
    }
    BYJCarouselTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"carouse"];
    BYJsongListModel *model= _modelArr[indexPath.row - 1];
    cell.model = model;
    cell.indexPath = indexPath.row;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DSYMusicViewController *musicVC = [DSYMusicViewController sharedMusicPlayingVC];
    
    if (indexPath.row == 0) {
        musicVC.index = 0;
    } else {
        
        musicVC.index = indexPath.row - 1;
    }
    musicVC.isLocal = NO;
    
    
    [PlayViewController sharePlayViewBar].hidden = YES;
    DSYNAVViewController *nav  = [DSYNAVViewController shareNavWithRootViewController:musicVC];
    [self presentViewController:nav animated:YES completion:nil];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

#pragma mark - 数据解析
- (void)dataHandle {
    /**小菊花*/
    UIView *hudView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
    hudView.center = CGPointMake(self.view.center.x, _tableView.frame.size.height / 2);
    [self.view addSubview:hudView];
    [MBProgressHUD showHUDAddedTo:hudView animated:YES];

    if ([self.type isEqualToString:@"13"]) {
        self.str = [NSString stringWithFormat:@"http://api.dongting.com/frontpage/module/songlist?moduleId=%@", self.ID];
    }
    if ([self.type isEqualToString:@"21"] ) {
        self.str = [NSString stringWithFormat:@"http://api.dongting.com/channel/channel/songs/scene/%@", self.ID];
    }
    if ([self.type isEqualToString:@"22"]) {
        self.str = [NSString stringWithFormat:@"http://api.dongting.com/channel/channel/songs/newest?app=ttpod&v=v8.1.5.2016022418&user_id=407639422&mid=iPhone7,2&f=f320&s=s310&imsi=&hid=&splus=9.2.1&active=1&net=2&openudid=921fb117b843753583dbf257222bb96a79d3c64e&idfa=5B1500CB-A041-4C95-951A-F6408B90A0BF&utdid=Vule3TyuSLwDAKd/fPxZrXTy&alf=201200&bundle_id=com.ttpod.music&latitude=38.8825797654297&longtitude=121.5392649179896&access_token=aaaa74e14fbd80b9ff753dce5dfb32d5"];
    }
//    if ([self.type isEqualToString:@"1"]) {
//        self.str = [NSString stringWithFormat:@" http://api.dongting.com/song/album/%@?", self.ID];
//    }
    [APPTools GETWithURL:_str par:nil success:^(id responseObject) {
        self.modelArr = [BYJsongListModel modelHandleWithArray:responseObject[@"data"]];
        // 往播放器传数据
        [DSYDataManager shareDataManager].allDataArray = [DSYSongModel modelHandleWithArray:responseObject[@"data"]];
        [MBProgressHUD hideHUDForView:hudView animated:YES];
        [hudView removeFromSuperview];
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];
}


- (void)playButtonAction:(UIButton *)button {
    
}

- (void)manageButtonAction:(UIButton *)button {
    
    BYJManageViewController *VC = [[BYJManageViewController alloc] init];
//    VC.dataSource = _model.songsArr;
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
